;;;
;;; JaCoLiB
;;; Copyright (C) 2008 Alessandro Serra
;;; 
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;; 
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;; 
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;

;;;; Created on 2008-07-22 10:33:15

(in-package :jacolib)

;
; RPC Client
;

(defclass rpc-client-stub ()
  ((exec-handler
    :INITARG :exec-handler
    :ACCESSOR rpc-client-stub-exec-handler
    :DOCUMENTATION "The handler used to send the request to the remote server.")
   (quit-handler
    :INITARG :quit-handler
    :ACCESSOR rpc-client-stub-quit-handler))
  (:documentation "A stub of a RPC client. 
It serializes the requests to be sent to a remote server and 
deserializes its responses."))

; functions 

(defun rpc-client-stub-exec (client-stub rpc-request)
  (declare (type rpc-client-stub client-stub)
           (type rpc-request rpc-request))
  (let ((rpc-response
         (json-serializer-decode-from-string 
          (funcall (rpc-client-stub-exec-handler client-stub)
                   (json-serializer-encode-to-string rpc-request)))))
    (if (not (typep rpc-response 'rpc-response))
        (error 'protocol-error :message "Expected a rpc-response object."))
    rpc-response))

(defun rpc-client-stub-quit (client-stub)
  (declare (type rpc-client-stub client-stub))
  (funcall (rpc-client-stub-quit-handler client-stub)))

; quiting

(defun rpc-client-stub-quit% (stub)
  (setf (slot-value stub 'exec-handler) nil)
  (setf (slot-value stub 'quit-handler) nil)
  nil)
